package net.jueb.object.server;

import net.jueb.object.pojo.Request;
import net.jueb.object.pojo.Response;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 * SimpleChannelInboundHandler是ChannelHandlerAdapter的子类
 * @author helin
 *
 */
public class ObjectServerHandler extends ChannelHandlerAdapter{
	
	private int count;//请求次数
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)throws Exception {
		//因为之前加入了解码器。所以可以直接使用对象
		Request req=(Request)msg;
		if(req==null)
		{
			return;
		}
		count++;
		System.out.println("收到请求："+req.toString());
		Response rep=new Response();
		rep.setId(count);
		rep.setResult("Hello Client！");
		//因为加入了编码器，所以可以直接写入对象
		ctx.writeAndFlush(rep);
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}
